package green.mapstore

import green.mapstore.HashMapStore

class HashMapStoreTest extends GroovyTestCase {

  HashMapStore store
  def key = 'key'
  def value = 'value'

  @Override
  void setUp() {
    store = new HashMapStore(System.getProperty("java.io.tmpdir") + "${getClass()}${new Date().getTime().toString() + new Random().nextInt(1000)}")
    store.init()
  }

  void testSaveEmptyStore() {
    // when - store is empty and doesn't exist
    assert store.@store.isEmpty()
    assert !store.existsOnDisk() : 'Store already exists on disk'

    // then - commit it
    store.commit()

    // assert - and it should exist now
    assert store.@pathFile.exists()
  }

  void testReadValueFromDisk_theSameClient() {
    // assert - when value is added, commited and store exists on the HDD
    store.add(key, value)
    store.commit()
    assert store.existsOnDisk()

    // then - read it
    def savedValue = store.get(key)

    // assert - and it should be the same as the written one
    assertEquals value, savedValue
  }
}
